#include "pgr.h"
#include "rectype.h"
#include "util.h"
#include "nmfield.h"
#include "cnfield.h"
#include "nmarrfield.h"

using namespace std;
using namespace opsrc::stdf;

Pgr::Pgr()
{
}

Pgr::Pgr(stdf_rec _stdfrec, int _reclen, std::ios::pos_type _fileRecFirstBytePosition, int _orderindex, endian e, char* buf)
    :RecBase(_stdfrec, _reclen, _fileRecFirstBytePosition, _orderindex),en(e) {
    if (buf!=0)
        readFields(buf);
}


Pgr::~Pgr() {
}

const StdfField<int> Pgr::getGroupIndex() const {
    return grp_indx;
}

void Pgr::setGroupIndex(StdfField<int> grpindx) {
    grp_indx = grpindx;
}

const StdfField<string> Pgr::getGroupName() const {
    return grp_nam;
}

void Pgr::setGroupName(StdfField<string> grpnam) {
    grp_nam = grpnam;
}

const StdfField<int> Pgr::getIndexCount() const {
    return indx_cnt;
}

void Pgr::setIndexCount(StdfField<int> indxcnt) {
    indx_cnt = indxcnt;
}

const StdfField<vector<unsigned int> > Pgr::getPinIndexArray() const {
    return pmr_indx;
}

void Pgr::setPinIndexArray(StdfField<vector<unsigned int> > pmrindx) {
    pmr_indx = pmrindx;
}

void Pgr::readFields(char *buf) {

    cout << "stdfrec= PGR , filebytestartposition= " <<   getFileRecByteStartPosition() << ", recbytecount= " << reclen <<endl;

    int curpos = 0;
    // Read group index , 2 byte
    NmField<unsigned int, 2> grpindx(buf+curpos, en);
    grp_indx.set(grpindx.getData(), curpos, grpindx.getByteCount(), grpindx.getTotalByteCount());
    curpos += grp_indx.getTotalByteCount();

    // read group name
    if (curpos < reclen-1) {
        CnField<string> grpnam(buf+curpos, en);
        grp_nam.set(grpnam.getData(), curpos, grpnam.getByteCount(), grpnam.getTotalByteCount());
        curpos +=  grp_nam.getTotalByteCount();
    }

    // read index count
    if (curpos < reclen-1) {
        NmField<unsigned int, 2> indxcnt(buf+curpos, en);
        indx_cnt.set(indxcnt.getData(), curpos, indxcnt.getByteCount(), indxcnt.getTotalByteCount());
        curpos += indx_cnt.getTotalByteCount();
    }


    // read pin indexes, U2
    if (curpos < reclen - 1) {
        NmArrField<unsigned int, 2> pmrindx(buf+curpos, en, indx_cnt.getData());
        pmr_indx.set(pmrindx.getData(), curpos, pmrindx.getByteCount(), pmrindx.getTotalByteCount());
    }
}
